﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Data.Sql;
using System.Data.SqlClient;
public partial class Main_Inventory_Payment_AgainstBill : System.Web.UI.Page
{
    Datautility du = new Datautility();
    List<string> LstCount = new List<string>();
    List<string> LstDdlType = new List<string>();
    List<string> LstDdlParticulars = new List<string>();
    List<string> LstTxtParticulars = new List<string>();
    List<string> LstTxtOriginalAmt = new List<string>();
    List<string> LstTxtPendingAmt = new List<string>();
    List<string> LstTxtAmount = new List<string>();
    List<string> LstTxtTotalAmt = new List<string>();
    List<string> LstTxtPendingAfterRec = new List<string>();
    int intRowCounter = 2;
    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        _OverrideNewRow();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] != string.Empty)
        {
            if (!IsPostBack)
            {
                _BindBank();
                _BindCurrency();
                Vendorslist();
            }
        }
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
    }
    private void _BindType(DropDownList ddl)
    {
        ddl.Items.Insert(0, new ListItem("- - Select Type - -", "0"));
        ddl.Items.Insert(1, new ListItem("On A/c", "1"));
        ddl.Items.Insert(2, new ListItem("Towards Bills", "2"));
    }
    private void _BindBank()
    {
        // Here the data is fetched from database on the basis of two tables ACC_LEDMAST and 
        DataTable dt = du.getdata("select LID,LNAME from ACC_LEDMAST L INNER JOIN acc_grpmast G ON L.PGID=G.GID  WHERE G.GROUPTREEID=002003001002");
        if (dt.Rows.Count > 0)
        {
            ddlBank.DataSource = dt;
            ddlBank.DataValueField = "LID";
            ddlBank.DataTextField = "LNAME";
            ddlBank.DataBind();
            ddlBank.Items.Insert(0, new ListItem("- - - - - - - - - - - - Select Bank - - - - - - - - - - - -", "0"));
        }
    }
    private void _BindCurrency()
    {
        DataTable DTCurrency = du.getdata("select * from mas_currency");
        ddlCurrency.DataSource = DTCurrency;
        ddlCurrency.DataTextField = "currency_name";
        ddlCurrency.DataValueField = "id";
        ddlCurrency.DataBind();
        ddlCurrency.Items.Insert(0, new ListItem("- - Select Currency - -", "0"));
    }
    private void Vendorslist()
    {
        DataTable dtvendorname = du.getdata("select LID,LNAME from ACC_LEDMAST L INNER JOIN acc_grpmast G ON L.PGID=G.GID  WHERE G.GROUPTREEID=001005002");
        if (dtvendorname.Rows.Count > 0)
        {
            ddlvendor.DataSource = dtvendorname;
            ddlvendor.DataValueField = "LID";
            ddlvendor.DataTextField = "LNAME";
            ddlvendor.DataBind();
            ddlvendor.Items.Insert(0, new ListItem("- - - - - - - - - - - - Select Vendor - - - - - - - - - - - -", "0"));
        }
    }
    private void _GetMaxRecieptNo(string strvendorId)
    {
        string strSql = "select (isNull(max(paymentnoid),0)+ 1) as PaymentNo from IVR_PAYMENTAGAINSTBILL_MAIN where vendorid='" + strvendorId + "'";
        DataTable DTpaymentno = du.getdata(strSql);
        if (DTpaymentno.Rows.Count > 0)
        {
            txtRecieptNumber.Text = DTpaymentno.Rows[0]["PaymentNo"].ToString();
        }
    }
    protected void ddlvendor_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlvendor.SelectedValue.ToString() != "0")
        {
            _GetMaxRecieptNo(ddlvendor.SelectedValue.ToString());
        }
    }
    protected void ddlCurrency_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlCurrency.SelectedValue.ToString() != "0")
        {
            if (ddlCurrency.SelectedValue.ToString() == "1")
            {
                txtExchangeRate.Enabled = false;
            }
            else
            {
                txtExchangeRate.Enabled = true;
            }
        }
    }
    private void _AddNewRow()
    {
        intRowCounter++;
        HtmlTableRow HTRow = new HtmlTableRow();

        HtmlTableCell HTCell1 = new HtmlTableCell();
        DropDownList ddlType = new DropDownList();
        ddlType.ID = "ddlType" + intRowCounter.ToString();
        ddlType.CssClass = "input";
        ddlType.Width = Unit.Percentage(98);
        _BindType(ddlType);

        HTCell1.Controls.Add(ddlType);
        ddlType.AutoPostBack = true;
        ddlType.SelectedIndexChanged += new EventHandler(ddlType_SelectedIndexChanged);
        LstCount.Add(intRowCounter.ToString());
        LstDdlType.Add(ddlType.ID);


        HtmlTableCell HTCell2 = new HtmlTableCell();
        DropDownList ddlParticulars = new DropDownList();
        ddlParticulars.ID = "ddlParticulars" + intRowCounter.ToString();
        ddlParticulars.CssClass = "input";
        ddlParticulars.Width = Unit.Percentage(98);
        ddlParticulars.Visible = false;
        ddlParticulars.AutoPostBack = true;
        ddlParticulars.Attributes.Add("onchange", "return ValidateRepeatParticulars(this.id);");
        ddlParticulars.SelectedIndexChanged += new EventHandler(ddlParticulars_SelectedIndexChanged);


        TextBox txtParticulars = new TextBox();
        txtParticulars.ID = "txtParticulars" + intRowCounter.ToString();
        txtParticulars.CssClass = "input";
        txtParticulars.Width = Unit.Percentage(98);
        HTCell2.Controls.Add(ddlParticulars);
        HTCell2.Controls.Add(txtParticulars);
        LstDdlParticulars.Add(ddlParticulars.ID);
        LstTxtParticulars.Add(txtParticulars.ID);

        HtmlTableCell HTCell3 = new HtmlTableCell();

        TextBox txtOriginalAmt = new TextBox();
        txtOriginalAmt.ID = "txtOriginalAmt" + intRowCounter.ToString();
        txtOriginalAmt.CssClass = "input";
        txtOriginalAmt.Width = Unit.Percentage(98);
        txtOriginalAmt.Attributes.Add("onkeydown", "return EnabledTabKey(this.id,event);");
        HTCell3.Controls.Add(txtOriginalAmt);
        LstTxtOriginalAmt.Add(txtOriginalAmt.ID);

        //HtmlTableCell HTCell4 = new HtmlTableCell();
        //TextBox txtPendingAmt = new TextBox();
        //txtPendingAmt.ID = "txtPendingAmt" + intRowCounter.ToString();
        //txtPendingAmt.CssClass = "input";
        //txtPendingAmt.Width = Unit.Percentage(98);
        //txtPendingAmt.Attributes.Add("onkeydown", "return EnabledTabKey(this.id,event);");
        //HTCell4.Controls.Add(txtPendingAmt);
        //LstTxtPendingAmt.Add(txtPendingAmt.ID);

        HtmlTableCell HTCell5 = new HtmlTableCell();
        TextBox txtAmount = new TextBox();
        txtAmount.ID = "txtAmount" + intRowCounter.ToString();
        txtAmount.CssClass = "input";
        txtAmount.Width = Unit.Percentage(98);
        txtAmount.Attributes.Add("onkeypress", "return isNumberKey(this.id,event);");
        txtAmount.Attributes.Add("onblur", "return CheckValidAmt(this.id);");
        HTCell5.Controls.Add(txtAmount);
        LstTxtAmount.Add(txtAmount.ID);



        //HtmlTableCell HTCell7 = new HtmlTableCell();
        //TextBox txtTotalAmt = new TextBox();
        //txtTotalAmt.ID = "txtTotalAmt" + intRowCounter.ToString();
        //txtTotalAmt.CssClass = "input";
        //txtTotalAmt.Width = Unit.Percentage(98);
        //txtTotalAmt.Attributes.Add("onkeydown", "return EnabledTabKey(this.id,event);");
        //HTCell7.Controls.Add(txtTotalAmt);
        //LstTxtTotalAmt.Add(txtTotalAmt.ID);


        HtmlTableCell HTCell8 = new HtmlTableCell();
        TextBox txtPendingAfterRec = new TextBox();
        txtPendingAfterRec.ID = "txtPendingAfterRec" + intRowCounter.ToString();
        txtPendingAfterRec.CssClass = "input";
        txtPendingAfterRec.Width = Unit.Percentage(98);
        txtPendingAfterRec.Attributes.Add("onkeydown", "return EnabledTabKey(this.id,event);");
        HTCell8.Controls.Add(txtPendingAfterRec);
        LstTxtPendingAfterRec.Add(txtPendingAfterRec.ID);


        ViewState["LstCount"] = LstCount;
        ViewState["LstDdlType"] = LstDdlType;
        ViewState["LstDdlParticulars"] = LstDdlParticulars;
        ViewState["LstTxtParticulars"] = LstTxtParticulars;
        ViewState["LstTxtOriginalAmt"] = LstTxtOriginalAmt;
        //ViewState["LstTxtPendingAmt"] = LstTxtPendingAmt;
        ViewState["LstTxtAmount"] = LstTxtAmount;
        //ViewState["LstTxtTotalAmt"] = LstTxtTotalAmt;
        ViewState["LstTxtPendingAfterRec"] = LstTxtPendingAfterRec;

        HTRow.Cells.Add(HTCell1);
        HTRow.Cells.Add(HTCell2);
        HTRow.Cells.Add(HTCell3);
        //HTRow.Cells.Add(HTCell4);
        HTRow.Cells.Add(HTCell5);
        //HTRow.Cells.Add(HTCell7);
        HTRow.Cells.Add(HTCell8);
        tblDetails.Rows.Add(HTRow);
        hidRowCount.Value = intRowCounter.ToString();
    }
    private void _OverrideNewRow()
    {
        LstCount = (List<string>)ViewState["LstCount"];
        LstDdlType = (List<string>)ViewState["LstDdlType"];
        LstDdlParticulars = (List<string>)ViewState["LstDdlParticulars"];
        LstTxtParticulars = (List<string>)ViewState["LstTxtParticulars"];
        LstTxtOriginalAmt = (List<string>)ViewState["LstTxtOriginalAmt"];
        //LstTxtPendingAmt = (List<string>)ViewState["LstTxtPendingAmt"];
        LstTxtAmount = (List<string>)ViewState["LstTxtAmount"];
        //LstTxtTotalAmt = (List<string>)ViewState["LstTxtTotalAmt"];
        LstTxtPendingAfterRec = (List<string>)ViewState["LstTxtPendingAfterRec"];
        if (LstCount != null)
        {
            int intCount = LstCount.Count;
            for (int i = 0; i < intCount; i++)
            {
                intRowCounter++;

                HtmlTableRow HTRow = new HtmlTableRow();

                HtmlTableCell HTCell1 = new HtmlTableCell();
                DropDownList ddlType = new DropDownList();
                ddlType.ID = LstDdlType[i].ToString();
                ddlType.CssClass = "input";
                ddlType.Width = Unit.Percentage(98);
                _BindType(ddlType);
                ddlType.AutoPostBack = true;
                ddlType.SelectedIndexChanged += new EventHandler(ddlType_SelectedIndexChanged);
                HTCell1.Controls.Add(ddlType);

                HtmlTableCell HTCell2 = new HtmlTableCell();
                DropDownList ddlParticulars = new DropDownList();
                ddlParticulars.ID = LstDdlParticulars[i].ToString();
                ddlParticulars.CssClass = "input";
                ddlParticulars.Width = Unit.Percentage(98);
                ddlParticulars.Visible = false;
                // _BindPendingParticular(ddlParticulars);
                ddlParticulars.AutoPostBack = true;
                ddlParticulars.Attributes.Add("onchange", "return ValidateRepeatParticulars(this.id);");
                ddlParticulars.SelectedIndexChanged += new EventHandler(ddlParticulars_SelectedIndexChanged);


                TextBox txtParticulars = new TextBox();
                txtParticulars.ID = LstTxtParticulars[i].ToString();
                txtParticulars.CssClass = "input";
                txtParticulars.Width = Unit.Percentage(98);
                HTCell2.Controls.Add(ddlParticulars);
                HTCell2.Controls.Add(txtParticulars);

                HtmlTableCell HTCell3 = new HtmlTableCell();

                TextBox txtOriginalAmt = new TextBox();
                txtOriginalAmt.ID = LstTxtOriginalAmt[i].ToString();
                txtOriginalAmt.CssClass = "input";
                txtOriginalAmt.Width = Unit.Percentage(98);
                txtOriginalAmt.Attributes.Add("onkeydown", "return EnabledTabKey(this.id,event);");
                HTCell3.Controls.Add(txtOriginalAmt);

                //HtmlTableCell HTCell4 = new HtmlTableCell();
                //TextBox txtPendingAmt = new TextBox();
                //txtPendingAmt.ID = LstTxtPendingAmt[i].ToString();
                //txtPendingAmt.CssClass = "input";
                //txtPendingAmt.Width = Unit.Percentage(98);
                //txtPendingAmt.Attributes.Add("onkeydown", "return EnabledTabKey(this.id,event);");
                //HTCell4.Controls.Add(txtPendingAmt);

                HtmlTableCell HTCell5 = new HtmlTableCell();
                TextBox txtAmount = new TextBox();
                txtAmount.ID = LstTxtAmount[i].ToString();
                txtAmount.CssClass = "input";
                txtAmount.Width = Unit.Percentage(98);
                txtAmount.Attributes.Add("onkeypress", "return isNumberKey(this.id,event);");
                txtAmount.Attributes.Add("onblur", "return CheckValidAmt(this.id);");
                HTCell5.Controls.Add(txtAmount);


                //HtmlTableCell HTCell7 = new HtmlTableCell();
                //TextBox txtTotalAmt = new TextBox();
                //txtTotalAmt.ID = LstTxtTotalAmt[i].ToString();
                //txtTotalAmt.CssClass = "input";
                //txtTotalAmt.Width = Unit.Percentage(98);
                //txtTotalAmt.Attributes.Add("onkeydown", "return EnabledTabKey(this.id,event);");
                //HTCell7.Controls.Add(txtTotalAmt);


                HtmlTableCell HTCell8 = new HtmlTableCell();
                TextBox txtPendingAfterRec = new TextBox();
                txtPendingAfterRec.ID = LstTxtPendingAfterRec[i].ToString();
                txtPendingAfterRec.CssClass = "input";
                txtPendingAfterRec.Width = Unit.Percentage(98);
                txtPendingAfterRec.Attributes.Add("onkeydown", "return EnabledTabKey(this.id,event);");
                HTCell8.Controls.Add(txtPendingAfterRec);


                HTRow.Cells.Add(HTCell1);
                HTRow.Cells.Add(HTCell2);
                HTRow.Cells.Add(HTCell3);
                //HTRow.Cells.Add(HTCell4);
                HTRow.Cells.Add(HTCell5);
                //HTRow.Cells.Add(HTCell7);
                HTRow.Cells.Add(HTCell8);
                tblDetails.Rows.Add(HTRow);
            }
        }
    }
    protected void btnAddMoreRow_Click(object sender, ImageClickEventArgs e)
    {
        _AddNewRow();
    }
    protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        int intRowIndex = Convert.ToInt32(ddl.ID.Substring(7).ToString());
        DropDownList ddlParticulars = (DropDownList)tblDetails.Rows[intRowIndex].FindControl("ddlParticulars" + intRowIndex.ToString());
        TextBox txtParticulars = (TextBox)tblDetails.Rows[intRowIndex].FindControl("txtParticulars" + intRowIndex.ToString());
        TextBox txtOriginalAmt = (TextBox)tblDetails.Rows[intRowIndex].FindControl("txtOriginalAmt" + intRowIndex.ToString());
        TextBox txtAmount = (TextBox)tblDetails.Rows[intRowIndex].FindControl("txtAmount" + intRowIndex.ToString());
        TextBox txtPendingAfterRec = (TextBox)tblDetails.Rows[intRowIndex].FindControl("txtPendingAfterRec" + intRowIndex.ToString());
        if (ddl.SelectedValue.ToString() != "0")
        {
            if (ddl.SelectedValue == "1")
            {
                ddlParticulars.Visible = false;
                txtParticulars.Visible = true;
                txtParticulars.Enabled = true;
            }
            else
            {
                hidvendorid.Value = ddlvendor.SelectedValue.ToString();
                ddlParticulars.Visible = true;
                txtParticulars.Visible = false;
                txtParticulars.Enabled = false;
                _binddropdown(Convert.ToDouble(hidvendorid.Value), ddlParticulars);
            }
            txtParticulars.Text = string.Empty;
            txtOriginalAmt.Text = string.Empty;
            txtAmount.Text = string.Empty;
            LblErrorMsg.Text = string.Empty;
        }        
    }
    protected void ddlParticulars_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        int intRowIndex = Convert.ToInt32(ddl.ID.Substring(14));
        DropDownList ddlParticulars = (DropDownList)tblDetails.Rows[intRowIndex].FindControl("ddlParticulars" + intRowIndex.ToString());
        TextBox txtParticulars = (TextBox)tblDetails.Rows[intRowIndex].FindControl("txtParticulars" + intRowIndex.ToString());
        TextBox txtOriginalAmt = (TextBox)tblDetails.Rows[intRowIndex].FindControl("txtOriginalAmt" + intRowIndex.ToString());
        TextBox txtAmount = (TextBox)tblDetails.Rows[intRowIndex].FindControl("txtAmount" + intRowIndex.ToString());
        TextBox txtPendingAfterRec = (TextBox)tblDetails.Rows[intRowIndex].FindControl("txtPendingAfterRec" + intRowIndex.ToString());
        if (ddlParticulars.SelectedValue.ToString() != "0")
        {
            string id = ddlParticulars.SelectedValue.ToString();
            DataTable dt = du.getdata("select itemtotal from IVR_TRAN_TOTAL where TRNID=" + id);
            if (dt.Rows.Count > 0)
            {
                txtOriginalAmt.Text = dt.Rows[0]["itemtotal"].ToString();
            }
        }
        else
        {
            txtOriginalAmt.Text = string.Empty;
        }
    }
    private void _binddropdown(double vendorid, DropDownList ddl)
    {
        if (vendorid > 0)
        {
            DataTable dtpurchaserec = du.getdata("select ACC_POMAIN.POPREFIX+'/'+ACC_POMAIN.POVOUCHERNO+'/'+ACC_POMAIN.POSUFFIX +'-'+IVR_TRANFORMATNUM.NUMPREFIX+'/'+IVR_TRAN_MAIN.TRNNUM+'/'+IVR_TRANFORMATNUM.NUMSUFFIX +'-'+IVR_TRAN_MAIN.INVOICENO as Particulars,IVR_TRAN_MAIN.TRNID from IVR_TRAN_MAIN inner join IVR_TRANFORMATNUM on IVR_TRANFORMATNUM.TFNID=IVR_TRAN_MAIN.TFOID inner join ACC_POMAIN on ACC_POMAIN.POID=IVR_TRAN_MAIN.POID where IVR_TRAN_MAIN.VENDCUST_LID=" + vendorid);
            if (dtpurchaserec.Rows.Count > 0)
            {
                ddl.DataSource = dtpurchaserec;
                ddl.DataValueField = "TRNID";
                ddl.DataTextField = "Particulars";
                ddl.DataBind();
                ddl.Items.Insert(0, new ListItem("--PoNo/VoucherNo/InvoiceNo--", "0"));
            }
        }
    }
    private void _SaveTrans()
    {
        try
        {
            if (ddlvendor.SelectedValue.ToString() != "0")
            {
                DataTable dtisnumberexits = du.getdata("select * from iVr_paymentagainstbill_Main where vendorid=" + ddlvendor.SelectedValue + " and paymentnoid='" + txtRecieptNumber.Text + "'");
                if (dtisnumberexits.Rows.Count > 0)
                {
                    LblErrorMsg.Text = "Payment no Already Exits";
                }
                else
                {
                    string bankname = string.Empty;
                    string[] strBankName;
                    string strAccNo = string.Empty;
                    //strAccNo = "436303011021";
                    if (ddlBank.SelectedValue.ToString() != "0")
                    {
                        strBankName = ddlBank.SelectedItem.ToString().Split('\\');
                        bankname = strBankName[0];
                        strAccNo = strBankName[1];
                    }
                    string strDate = string.Empty;
                    if (txtDate.Text != string.Empty)
                    {
                        string[] strArr = txtDate.Text.Split('/');
                        strDate = strArr[1] + "/" + strArr[0] + "/" + strArr[2];
                    }
                    if (strDate != string.Empty)
                    {

                        if (txtRecievedAmt.Text.ToString() != string.Empty && Convert.ToDouble(txtRecievedAmt.Text) > 0)
                        {
                            SqlParameter[] sparam1 ={
                                      
                                       new SqlParameter("@ID","0"),
                                       new SqlParameter("@RECIEPTNO",txtRecieptNumber.Text.Trim()),
                                       new SqlParameter("@DATE",strDate),
                                       new SqlParameter("@Vendorid",ddlvendor.SelectedValue),
                                       new SqlParameter("@vendorname",ddlvendor.SelectedItem.ToString()),   
                                       new SqlParameter("@BANK_NAME",bankname),
                                       new SqlParameter("@BANK_ACCNO",strAccNo),
                                       new SqlParameter("@REF_NO",txtRefNo.Text.Trim()),
                                       new SqlParameter("@CURRENCY",ddlCurrency.SelectedValue),
                                       new SqlParameter("@EXCHANGE_RATE",txtExchangeRate.Text.Trim()),
                                       new SqlParameter("@REMARKS",txtRemarks.Text),
                                       new SqlParameter("@RECIEVED_AMT",txtRecievedAmt.Text.Trim()),
                                       new SqlParameter("@RECIEPT_MODE",ddlRecieptMode.SelectedItem.Text),
                                       new SqlParameter("@CNAME",Session["username"].ToString()) ,
                                       new SqlParameter("@ChequeDDDate",Convert.ToDateTime(txtChequeddDate.Text))                                    
                                   };
                            int Result1 = du.executeprocedure("USP_SavePaymentAgainstBill_Main", sparam1);
                            if (Result1 > 0)
                            {
                                DataTable dtmaxid = du.getdata("select max(id) as id from ivr_paymentagainstbill_main");
                                if (dtmaxid.Rows.Count > 0)
                                {
                                    _SavePaymentDetails(dtmaxid.Rows[0]["id"].ToString());
                                    LblErrorMsg.Text = "Value saved successfully";
                                    Response.Redirect("Payment_AgainstBill.aspx");
                                    Emptycontrols();
                                }
                            }
                        }
                        else
                        {
                            LblErrorMsg.Text = "Recived Amount Cant Be Empty";
                        }
                    }
                    else
                    {
                        LblErrorMsg.Text = "Please Select Date";
                    }
                }
            }
        }
        catch (Exception ex)
        {
            LblErrorMsg.Text = ex.Message.ToString();
        }
    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        _SaveTrans();
    }
    protected void Emptycontrols()
    {
        txtRemarks.Text = string.Empty;
        txtRefNo.Text = string.Empty;
        txtRecievedAmt.Text = string.Empty;
        txtRecieptNumber.Text.ToString();
        txtExchangeRate.Text = string.Empty;
        ddlCurrency.SelectedValue = "0";
        ddlBank.SelectedValue = "0";
        ddlRecieptMode.SelectedValue = "0";
        ddlvendor.SelectedValue = "0";
        txtDate.Text = string.Empty;
    }
    private void _SavePaymentDetails(string strRecieptId)
    {
        try
        {
            if (strRecieptId != string.Empty)
            {
                int intRowCount = Convert.ToInt32(hidRowCount.Value);
                for (int i = 1; i <= intRowCount; i++)
                {
                    DropDownList ddlType = (DropDownList)tblDetails.Rows[i].FindControl("ddlType" + i.ToString());
                    DropDownList ddlParticulars = (DropDownList)tblDetails.Rows[i].FindControl("ddlParticulars" + i.ToString());
                    TextBox txtParticulars = (TextBox)tblDetails.Rows[i].FindControl("txtParticulars" + i.ToString());
                    TextBox txtAmount = (TextBox)tblDetails.Rows[i].FindControl("txtAmount" + i.ToString());
                    if (ddlType.SelectedValue != "0" && txtAmount.Text != string.Empty && Convert.ToDouble(txtAmount.Text.Trim()) > 0)
                    {

                        if (txtAmount.Text == string.Empty)
                        {
                            txtAmount.Text = "0";
                        }
                        string strParticulars = string.Empty;
                        string strBillId = string.Empty;
                        if (ddlType.SelectedValue == "2")
                        {
                            strParticulars = ddlParticulars.SelectedItem.Text;
                            strBillId = ddlParticulars.SelectedValue;
                        }
                        else
                        {
                            strParticulars = txtParticulars.Text.Trim();
                        }
                        SqlParameter[] sparam ={
                                       new SqlParameter("@ID","0"),
                                       new SqlParameter("@RECIEPT_ID",strRecieptId),
                                       new SqlParameter("@RECIEPT_TYPE",ddlType.SelectedValue),
                                       new SqlParameter("@PARTICULARS",strParticulars),
                                       new SqlParameter("@BILL_ID",strBillId),
                                       new SqlParameter("@AMOUNT",txtAmount.Text.Trim()),
                                                                                                                                                   
                                   };
                        int Result = du.executeprocedure("USP_PaymentAgainstBill_Details", sparam);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}